package com.pda.yuanjing.pages.sybm;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.JDBillBeanOut;
import com.public_module.tools.StringProcess;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CBillAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<JDBillBeanOut.DataBean> mlists = new ArrayList<>();
    private int selectedPosition = -1;

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public CBillAdapter(Context context, List<JDBillBeanOut.DataBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_inbill, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv1.setText("批次:" + mlists.get(position).getBatch());
        viewHolder.tv4.setText(mlists.get(position).getShowName1());

        if (!StringProcess.stringIsZero(mlists.get(position).getShowName1())) {
            viewHolder.tv4.setVisibility(View.VISIBLE);
            viewHolder.tv4.setText(mlists.get(position).getShowName1());
        }else {
            viewHolder.tv4.setVisibility(View.GONE);
        }

        viewHolder.tv2.setText("时间:" + mlists.get(position).getDoTime().split(" ")[0]);

        String str = "状态:" + mlists.get(position).getBillState();
        if (mlists.get(position).getBillState().equals("初始订单")) {
            viewHolder.tv3.setText(StringProcess.stringForegroundColorSpan(str, 3, "#ff6362"));
        } else {
            viewHolder.tv3.setText(StringProcess.stringForegroundColorSpan(str, 3, "#ffb438"));
        }
        if (selectedPosition == position) {
            viewHolder.ll.setBackgroundColor(Color.parseColor("#8a8a8a"));
        } else {
            viewHolder.ll.setBackgroundColor(Color.WHITE);
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv4)
        TextView tv4;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.tv3)
        TextView tv3;
        @BindView(R.id.ll)
        RelativeLayout ll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

